package com.twu.user;




import com.twu.io.IOHandler;

import java.util.ArrayList;

/**
 * Created by thoughtworks on 4/8/14.
 */
public class UserDatabase {
    private ArrayList<User> users = new ArrayList();
    private IOHandler io = new IOHandler();
    private int loggedInUser;

    public int login(String username, String password) {
        for(User user : users){
            if(user.getUsername().equalsIgnoreCase(username))
            {
                if(user.checkPassword(password)){
                    return user.getUid();
                }
                return 0;
            }
        }
        return 0;
    }

    public int addCustomer(String username, String password) {
        users.add(new User(username,password, User.userType.CUSTOMER));
        return users.get(users.size() - 1).getUid();
    }
    public void addAdmin(String username, String password) {
        users.add(new User(username,password, User.userType.ADMIN));
    }
    public User getUserById(int loggedInUser) {
        for(User user : users)
        {
            if(user.getUid() == loggedInUser)
                return user;
        }
        return  null;
    }
    public User getUserByName(String name) {
        for(User user : users)
        {
            if(user.getName() == name)
                return user;
        }
        return  null;
    }
    public void displayUserInfo() {
        User user = getUserById(loggedInUser);
        io.displayString(user.getDetails());
    }
}
